﻿using Fleck;
using MediatR;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WeChatRobotSDKModel.MediatRModel;
using WeChatRobotSDKModel.Model;

namespace WeChatRobotSDKServer
{
    /// <summary>
    /// 消息处理
    /// </summary>
    public class SocketResponsHandler : INotificationHandler<MediatRSocketRespons>
    {
        private WeChatRobotServer _weChatRobotServer;
        public SocketResponsHandler(WeChatRobotServer weChatRobotServer)
        {
            _weChatRobotServer = weChatRobotServer;
        }


        public Task Handle(MediatRSocketRespons notification, CancellationToken cancellationToken)
        {

            HandlerMessage(notification);
            return Task.CompletedTask;
        }

        public void HandlerMessage(MediatRSocketRespons notification)
        {
            if (notification.Error != 0) return;
            if (notification.Data == null) return;

            MsgModel msgModel = notification.Data.ToObject<MsgModel>();
            switch (msgModel.Type)
            {
                //todo:根据类型处理不同的消息
                case 1:
                case 2:
                    SendMM(msgModel);
                    break;
                default:
                    //ReceiveOtherIMEvent?.Invoke(connection, respons.Data.ToObject<ReceiveOtherIMArgs>());
                    break;
            }
        }


        public void SendMM(MsgModel msgModel)
        {
            //zixinwujia   wxid_85cwoh1o20r821  gh_6d934f80e6b4 18711008083@chatroom
            //if (msgModel.FromWxid == "onlyyou0730mm")
            //{
            //    _weChatRobotServer.SendTextMessage(msgModel.FromWxid, "好的");

            //    var res = _weChatRobotServer.SendTextMessageRes("gh_6d934f80e6b4", msgModel.Content);
            //}

            if (msgModel.FromWxid == "luoguoqi123")
            {

                _weChatRobotServer.SendTextMessage("gh_6d934f80e6b4", msgModel.Content);
            }

            if (msgModel.FromWxid == "gh_6d934f80e6b4")
            {
                _weChatRobotServer.SendTextMessage("luoguoqi123", msgModel.Content);
            }
        }
    }
}
